package com.example.skillcinema.adapters

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.skillcinema.databinding.FilterItemBinding
import com.example.skillcinema.db.displaymodels.FilterForDisplay
import com.example.skillcinema.viewholders.FilterItemViewHolder

class FilterRecyclerViewAdapter(val onClick: (id: Int, name: String) -> Unit) :
    RecyclerView.Adapter<FilterItemViewHolder>() {
    private var values: List<FilterForDisplay> = listOf()
    fun setData(list: List<FilterForDisplay>) {
        values = list
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FilterItemViewHolder =
        FilterItemViewHolder(
            FilterItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        )

    override fun getItemCount(): Int = values.count()

    override fun onBindViewHolder(holder: FilterItemViewHolder, position: Int) {
        val item: FilterForDisplay = values[position]
        with(holder.binding) {
            filterTextView.text = item.name
            filterTextView.setOnClickListener {
                onClick(item.id, item.name)
            }
        }
    }
}